﻿$(document).ready(function () {
    $("#btnSave:first").click(function (e) {
        e.preventDefault();
        var lblMessage = $("#lblMessage:first").text("");
        $(".required:visible").each(function () {
            $(this).removeClass("ui-state-error");
            if ($(this).val() == "") {
                lblMessage.text("É necessário preencher os campos obrigatórios.");
                $(this).addClass("ui-state-error");
            }
        });

        if (lblMessage.text() == "") {
            var customers = "";
            $("#cboCustomersLinked option").each(function () {
                customers += $(this).val() + ";";
            });
            $.ajax({
                dataType: "json",
                type: "POST",
                url: "/Budget/Save/" + $("#hdfIdBudget").val(),
                data: {
                    code: $("#hdfCode").val(),
                    id_trip: $("#cboTrip option:selected").val(),
                    validate_date: $("#txtValidateDate").val(),
                    value: $("#txtValue").val(),
                    description: $("#txtDescription").val(),
                    customers: customers
                },
                success: function (json) {
                    document.location.href = "/Budget/Index";
                },
                error: function (json) {
                    jAlert("Erro", "Erro", function () { return false; });
                }
            });
        };
    });

    var moveRight = function (e) {
        e.preventDefault();
        var cbo = $("#cboCustomers option:selected");
        if ($.trim(cbo.val()) != "") {
            var options = "";
            $("#cboCustomers option:selected").each(function () {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            });
            if (options != "") {
                $("#cboCustomersLinked").append(options);
            }
        }
    };

    var moveLeft = function (e) {
        e.preventDefault();
        var cbo = $("#cboCustomersLinked option:selected");
        if ($.trim(cbo.val()) != "") {
            var options = "";
            $("#cboCustomersLinked option:selected").each(function () {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            });
            if (options != "") {
                $("#cboCustomers").append(options);
            }
        }
    }

    var moveAll = function (e) {
        e.preventDefault();
        var options = "";
        $("#cboCustomers option").each(function () {
            if ($.trim($(this).val()) != "") {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            }
        });
        if (options != "") {
            $("#cboCustomersLinked").append(options);
        }
    }

    var backAll = function (e) {
        e.preventDefault();
        var options = "";
        $("#cboCustomersLinked option").each(function () {
            if ($.trim($(this).val()) != "") {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            }
        });
        if (options != "") {
            $("#cboCustomers").append(options);
        }
    }

    $("#lnkMoveRight").click(function (e) {
        moveRight(e);
    });

    $("#lnkMoveLeft").click(function (e) {
        moveLeft(e);
    });

    $("#lnkMoveAll").click(function (e) {
        moveAll(e);
    });

    $("#lnkBackAll").click(function (e) {
        backAll(e);
    });

    $("#cboTrip:first").focus();
});